<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Raul
 * Date: 5/29/13
 * Time: 4:13 PM
 * To change this template use File | Settings | File Templates.
 */
define('BASEPATH', true);
define('PROJECT', 'project');
define('ENV', 'master');
define('TABLE_TAG', 'crawl_log_');
date_default_timezone_set('Asia/Shanghai');
$projectPath = dirname(dirname(__FILE__));
require_once($projectPath . '/application/config/database.php');
require_once($projectPath . '/application/config/setting.php');
$db = new mysqli($db[ENV]['hostname'], $db[ENV]['username'], $db[ENV]['password'], $db[ENV]['database']);
$project_name = $config[PROJECT];
create_table();
function create_table() {
    global $db;
    $table_name = get_table_name();
    $create_query_string = create_query($table_name);

    $query = $db->query($create_query_string);
    var_dump($query);
    if($query) {
        echo "Create\tLog\tTable\t".$table_name."\tOK\tAt\t".date("Y-m-d H:i:s")."\n";
    } else {
        create_table();
    }
}

function get_table_name() {
    global $project_name;
    return $table_name = TABLE_TAG.$project_name.get_date_tag();
}

function get_date_tag() {
    $time = mktime(0, 0, 0, date('m'), date('d'), date('Y')) + 60 * 60 * 24;
    return $data_tag = date("_Y_m_d", $time);
}

function create_query($table_name) {
    $query_string = "CREATE TABLE `".$table_name."` (
                        `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增ID',
                        `token` char(32) DEFAULT NULL COMMENT '授权编码',
                        `key_word_hash` char(32) NOT NULL DEFAULT '' COMMENT '关键词hash',
                        `key_word_page_hash` char(32) NOT NULL COMMENT '关键词hash',
                        `key_word` varchar(200) NOT NULL COMMENT '关键词',
                        `page` smallint(1) NOT NULL DEFAULT '1' COMMENT '页数',
                        `original_page` int(11) NOT NULL COMMENT '原始页数',
                        `platform` enum('sina','tencent','sohu','netease','yunyun','other') NOT NULL DEFAULT 'sina' COMMENT '平台',
                        `use_ip` varchar(25) NOT NULL DEFAULT '' COMMENT '使用的ip',
                        `ip_type` enum('proxy','mobility') NOT NULL DEFAULT 'proxy' COMMENT 'ip类型',
                        `ip_error` varchar(200) DEFAULT NULL COMMENT 'ip错误',
                        `start_time` int(11) NOT NULL COMMENT '开始抓取时间',
                        `end_time` int(11) NOT NULL COMMENT '抓取结束时间',
                        `curl_result` smallint(1) NOT NULL COMMENT '抓取结果 1成功 2 CURL错误 3返回结果不完整 4其他 ',
                        `curl_return` longtext COMMENT 'curl返回数据',
                        `crawl_return` longtext COMMENT '抓取判断结果',
                        PRIMARY KEY (`id`),
                        KEY `key_word_hash` (`key_word_hash`),
                        KEY `key_word_page_hash` (`key_word_page_hash`),
                        KEY `platform` (`platform`),
                        KEY `use_ip` (`use_ip`),
                        KEY `ip_type` (`ip_type`),
                        KEY `start_time` (`start_time`),
                        KEY `end_time` (`end_time`),
                        KEY `curl_result` (`curl_result`),
                        KEY `token` (`token`)
                        ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;";
    return $query_string;
}